package com.kevin.utility;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class My {
	private static ExecutorService executor = Executors.newCachedThreadPool();// 定义一个线程池

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Boolean result = false;
		Future<Boolean> future = executor.submit(new MyJob());// 将任务提交到线程池中
		try {
			result = future.get(20000, TimeUnit.MILLISECONDS);// 设定在200毫秒的时间内完成
//			future.cancel(true);
		} catch (InterruptedException e) {
			System.out.println("InterruptedException");
		} catch (ExecutionException e) {
			System.out.println("ExecutionException");
		} catch (TimeoutException e) {
			System.out.println("Time is out");// 抛出超时异[size=xx-small][/size]常时打印
//			future.cancel(false);
			if(!future.isCancelled()){
				future.cancel(true);
			}
		}
		executor.shutdown();
		System.out.println(result);
	}
}
